package com.project.erp.dao.crm.customer;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.project.erp.common.dao.HibernateDao;
import com.project.erp.common.utils.Page;
import com.project.erp.model.crm.customer.CrmCustomer;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：客户关系-客户信息管理

 *  功能描述：客户信息管理Dao类

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-12-7 下午3:16:00
 */
@Repository
public class CrmCustomerDao extends HibernateDao<CrmCustomer, Long> {
	/**
	 * 根据id列表删除
	 * @param idsList
	 */
	public void delCrmCustomerBycrmCustomerIds(final List<Long> idsList){
		this.deleteByIdsList(idsList);
	}
	
	/**
	 * 根据系统参数标识查找系统参数记录
	 * @param postNum
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CrmCustomer getCrmCustomerByCusNameEn(String cusNameEn){
		Criteria c = this.getSession().createCriteria(CrmCustomer.class);
		c.add(Restrictions.eq("cusNameEn", cusNameEn));

		List<CrmCustomer> crmCustomers = c.list();
		CrmCustomer crmCustomer = null;
		
		if((crmCustomers != null) && (crmCustomers.size() > 0)){
			crmCustomer = crmCustomers.get(0);
		}
		return crmCustomer;
	}
	
	/**
	 * 根据条件查询
	 * @crmCustomer page
	 * @crmCustomer crmCustomer
	 * @return
	 */
	public Page<CrmCustomer> getCrmCustomerPageBySearch(Page<CrmCustomer> page,CrmCustomer crmCustomer){
		List<Criterion> list = new ArrayList<Criterion>();
		if(StringUtils.isNotEmpty(crmCustomer.getCusNameEn())){
			Criterion cusNameEn = Restrictions.ilike("cusNameEn",crmCustomer.getCusNameEn(),MatchMode.ANYWHERE);
			list.add(cusNameEn);
		}
		if(StringUtils.isNotEmpty(crmCustomer.getCusNameZh())){
			Criterion cusNameZh = Restrictions.ilike("cusNameZh",crmCustomer.getCusNameZh(),MatchMode.ANYWHERE);
			list.add(cusNameZh);
		}
		
		page = this.findPage(page,list.toArray(new Criterion[list.size()]));
		
		return page;
	}
}
